<?php

namespace Biz\service\admin;

use Biz\model\store\StoreCheck as mCheck;
use Biz\model\store\StoreBasic as mBasic;


class StoreCheck
{
    public  static function getListByWhere($where , $limit)
    {
        $list =   mCheck::getListByAdmin($where , $limit);
        if($list && is_array($list)){
            $store_ids = array_column($list , 'store_id');
            $store_names = mBasic::getByIds($store_ids , ['id' , 'name']);
            $store_names = array_combine(array_column($store_names , 'id') , $store_names);
            foreach($list as &$val){
                $val['check_status_name'] = mCheck::CHECK_MAP[$val['check_status']];
                $val['store_name'] = $store_names[$val['store_id']]['name'];
            }
        }
        $where['alias_status'] = mCheck::STATUS_NORAML;
        $count = mCheck::count($where);
        return ['list' => $list , 'count' => $count];
    }

    public static function updateCheck($data , $id)
    {
        return  mCheck::updateById($id , $data);
    }

    public static function InsertCheck($data)
    {
        return mCheck::add($data);
    }

    public static function getInfoById($id)
    {
        return mCheck::getInfo($id);
    }


    /*获取审核状态
     *
     * */
    public static function getCheckStatusByAlias($store_id , $alias)
    {
        $result = [];
        $check_info =  mCheck::getListByWhere(['store_id' => $store_id ,'alias' => $alias]);
        if($check_info && is_array($check_info)){
             foreach ($check_info as $val){
                 $result[] = [
                     'alias' =>  $val['alias'],
                     'check_status' => $val['check_status']
                 ];
             }
        }
        return $result;
    }


    public static function updateCheckByStoreId($store_id , $data)
    {
         $alias_info  = mCheck::getAliasByStoreId($store_id);
         $alias_arr = json_decode($data['alias'] , true);
         if($alias_info && is_array($alias_info)){
              foreach ($alias_info as $val){
                  if($val['check_status'] != mCheck::CHECK_DEFAULT) continue;
                  if(!in_array($val['alias'] , $alias_arr)){
                    mCheck::updateById($val['id'] , ['alias_status' => mCheck::STATUS_DELET]);
                  }
              }
         }
         $all_alias = array_column($alias_info , 'alias')?: [];
         $diff_alias = array_diff($alias_arr , $all_alias);
         if($diff_alias && is_array($diff_alias)){
             foreach($diff_alias as $alias){
                 mCheck::add([
                     'store_id' => $store_id,
                     'alias' => $alias,
                     'addtime' => date("Y-m-d H:i:s" , time())
                 ]);
             }
         }
    }

    /*更新审核失败的数据
     *
     * */
    public static function updateStoreAlias($alias , $store_id)
    {
        $basic_info = mBasic::getStoreInfoById($store_id);
        $alias_arr = explode("," , $basic_info['alias']);
        $diff_alias = array_diff($alias_arr , [$alias]);
        $alias_str = $diff_alias ? implode("," , $diff_alias) : '';
        mBasic::updateById($store_id , ['alias' => $alias_str]);
    }

}